package com.tianya.controller;

import com.tianya.common.Results;
import com.tianya.domain.dto.CourseRequestDTO;
import com.tianya.domain.entity.Course;
import com.tianya.domain.vo.CourseVO;
import com.tianya.domain.vo.PageResultVO;
import com.tianya.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author TianYaGuoKe
 * @date 2025/5/24 21:19
 * @description CourseController
 */
@Api("课程管理")
@RestController
@RequestMapping("/course")
public class CourseController {

    @Resource
    private CourseService courseService;


    @ApiOperation("查询所有课程")
    @GetMapping("/list")
    public Results<List<CourseVO>> list() {
        return Results.success(courseService.listCourses());
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Results<PageResultVO<CourseVO>> listPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                    @RequestBody CourseRequestDTO courseRequestDTO) {
        return Results.success(courseService.pageList(pageNum, pageSize, courseRequestDTO));
    }

    @ApiOperation("创建课程")
    @PostMapping
    public Results createCourse(@RequestBody CourseRequestDTO courseCreateDTO) {
        courseService.createCourse(courseCreateDTO);
        return Results.success();
    }
    @ApiOperation("查看详情")
    @GetMapping("/{id}")
    public Results<Course> getCourse(@PathVariable Long id) {
        return Results.success(courseService.getById(id));
    }
    @ApiOperation("编辑课程")
    @PutMapping("/{id}")
    public Results editCourse(@PathVariable Long id, @RequestBody Course course) {
        course.setCourseId(id);
        courseService.updateById(course);
        return Results.success();
    }
    @ApiOperation("删除课程")
    @DeleteMapping("/{id}")
    public Results deleteCourse(@PathVariable Long id) {
        courseService.removeById(id);
        return Results.success();
    }
}
